/**
 * Session管理初始化
 */
var Session = {
    id: "SessionTable",	//表格id
    table: null,
    sessionID:""
};

/**
 * 初始化表格的列
 */
Session.initColumn = function () {
    return [
        {title: 'sessionId', field: 'id',visible: false,  align: 'center', valign: 'middle', sortable: true},
        {title: '用户ID', field: 'userId',visible: false,  align: 'center', valign: 'middle', sortable: true},
        {title: '部门', field: 'deptName', align: 'center', valign: 'middle', sortable: true},
        {title: '用户名', field: 'name', align: 'center', valign: 'middle', sortable: true},
        {title: '职务', field: 'positionName', align: 'center', valign: 'middle', sortable: true},
        {title: '登录名', field: 'loginName', align: 'center', valign: 'middle', sortable: true},
        {title: 'IP地址', field: 'ipaddr', align: 'center', valign: 'middle', sortable: true,width:100},
        {title: '操作系统', field: 'os', align: 'center', valign: 'middle', sortable: true,width:100},
        {title: '浏览器', field: 'browser', align: 'center', valign: 'middle', sortable: true,width:150},
        {title: '状态', field: 'status', align: 'center', valign: 'middle', sortable: true,width:60,formatter:function(value,row,index){
            if (value == 'on_line') return '<span class="label label-success">在线</span>';
            if (value == 'off_line') return '<span class="label label-danger">离线</span>';
        }},
        {title: '登录时间', field: 'startTimestamp', align: 'center', valign: 'middle', sortable: true,width:160},
        {title: '最后访问时间', field: 'lastAccessTime', align: 'center', valign: 'middle', sortable: true,width:160},
        {title: '操作', field: 'id', align: 'center', valign: 'middle', sortable: true,width:70,formatter:function(value,row,index){
            if(row.id==Session.sessionID){
                return "<button class='btn btn-success btn-xs'>自己</button>";
            } else {
                return "<button class='btn btn-danger btn-xs' onclick='Session.offline(\"" + row.id + "\")'>踢下线</button>";
            }
        }},
    ];
};

/**
 * 下线
 */
Session.offline = function (id) {
    var operation = function(){
        var ajax = new $ax(Sys.backendPath + "/session/kickout", function (data) {
            Sys.success("下线成功!");
            Session.table.refresh();
        }, function (data) {
            Sys.error("下线失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id",id);
        ajax.start();
    };
    Sys.confirm("是否将用户踢下线?",operation);
};

/**
 * 搜索用户
 */
Session.search = function () {
    var queryData = {};
    queryData['searchKey'] = $("#searchKey").val();
    Session.table.refresh({query: queryData});
}


$(function () {
    var ajax = new $ax(Sys.backendPath + "/session/sessionId", function(data){
        Session.sessionID = data;
    },function(data){
    });
    ajax.start();
    var defaultColunms = Session.initColumn();
    var table = new BSTable(Session.id, Sys.backendPath + "/session/list", defaultColunms);
    table.setPaginationType("client");
    table.setSubHeight(10);
    table.setSort("lastAccessTime");
    Session.table = table.init();
});
